<?php
/**------------------------------------------------------------------------
 * @Author:      lixw
 * @DateTime:    2017-01-11 14:39:15
 * @Description: Description
 * Copyright (c) 2016 All rights reserved.
 *------------------------------------------------------------------------*/
namespace app\wap\controller;

class UserSetting extends UserBase{

	// 修改基本信息
	public function change_info(){
		$this->is_header       = false;
		$this->is_header_nav   = true;
		$this->is_foot_bartab  = false;
		$this->add_step('修改基本信息');
		if($this->isPost()){
			$data['realname']    = $this->_post('realname', '请填写姓名！');
			$data['sex']         = $this->_post("sex", '请选择性别！');
			$data['province_id'] = $this->_post('province_id', '请选择省份！');
			$data['city_id']     = $this->_post('city_id', '请选择城市！');

			// 名称判断
			if(model('Config')->is_name_filter($data['realname'])){
				$this->error('名称不能包含“' . model('Config')->tips_info . '”字符');
			}
			
			$where['user_id'] = $this->_user_id;
			model('User')->update($data, $where, true);
			// 更新登录信息
			model('User')->get_user_info(true);
			$this->success('修改成功！', url('UserCenter/index'));
		}

		return $this->view();
	}

	// 修改旧密码
	public function change_password(){
		$this->is_header       = false;
		$this->is_header_nav   = true;
		$this->is_foot_bartab  = false;
		$this->add_step($this->user['is_set_password'] ? '修改密码' : '密码设置');

		if($this->isPost()){
			$old_password = $this->user['is_set_password'] ? $this->_post('old_password', "旧密码不能为空！") : '';
			$new_password = $this->_post('new_password', "密码不能为空！");
			$repassword   = $this->_post('repassword', "确认密码不能为空！");

			// 检查两次密码一致性
			if($repassword != $new_password){
				$this->error('两次密码不一致！');
			}

			// 登陆密码对不对
			if(!model("User")->check_password($this->_user_id, $old_password) && $this->user['is_set_password']){
				$this->error("旧密码错误，请检查后再试！");
			}
			// 修改密码
			if(model("User")->change_password($this->_user_id, $old_password, $new_password)){
				$this->success("修改成功", url('UserCenter/index'));
			}else{
				$this->error(model("User")->tips_info);
			}
		}

		return $this->view();
	}

	// 更换手机号
	public function change_mobile(){
		$this->is_header       = false;
		$this->is_header_nav   = true;
		$this->is_foot_bartab  = false;
		$this->add_step(($this->user['mobile'] ? '更换' : '绑定') . '手机号');
		if($this->isPost()){
			$mobile  = $this->_post('mobile', '手机号不能为空！');
			$smscode = $this->_post('smscode', '短信验证码不能为空！');
			if(model('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}
			
			if($this->user['mobile'] && $this->_is_set_password){
				$password = $this->_post('password', '密码不能为空！');
				if(!model('User')->check_password($this->_user_id, $password)){
					$this->error(model('User')->tips_info);
				}
			}

			if(model('User')->change_mobile($this->_user_id, $mobile)){
				$this->success('修改成功！', url('UserCenter/index'));
			}else{
				$this->error(model('User')->tips_info);
			}
		}

		return $this->view();
	}

}
